#include "SQLSubPartitionByRange.h"
#include "../../sql/ast/SQLExpr.h"
#include "../../sql/visitor/SQLASTVisitor.h"
#include "../../sql/ast/expr/SQLIdentifierExpr.h"
#include "../../utils/instanceof.h"
#include "../../utils/listTran.h"
#include "../../utils/log.h"

SQLSubPartitionByRange::SQLSubPartitionByRange()
{
  columns = std::make_shared<std::list<SQLExpr_ptr>>();
}

SQLExpr_list_ptr SQLSubPartitionByRange::getColumns()
{
  return columns;
}

// @Override
void SQLSubPartitionByRange::accept0(SQLASTVisitor_ptr v)
{
  if (v->visit(SharedObject(SQLSubPartitionByRange)))
  {
    SQLObject_list_ptr tmp = ListTran::tran_class<SQLExpr, SQLObject>(columns);
    acceptChild(v, tmp);
    acceptChild(v, subPartitionsCount);
  }
  v->endVisit(SharedObject(SQLSubPartitionByRange));
}

SQLObject_ptr SQLSubPartitionByRange::clone()
{
  SQLObject_ptr clone_tmp = SQLSubPartitionByRange_ptr(new SQLSubPartitionByRange());
  SQLSubPartitionByRange_ptr x = std::dynamic_pointer_cast<SQLSubPartitionByRange>(clone_tmp);
  // SQLSubPartitionByRange *x = new SQLSubPartitionByRange();

  for (SQLExpr_ptr column : *columns)
  {
    SQLExpr_ptr c2 = std::dynamic_pointer_cast<SQLExpr>(column->clone());
    c2->setParent(x);
    x->columns->push_back(c2);
  }

  return x;
}

BOOL_ptr SQLSubPartitionByRange::isPartitionByColumn(long columnNameHashCode64)
{
  for (SQLExpr_ptr column : *columns)
  {
    if (instanceof <SQLExpr, SQLIdentifierExpr>(column) &&
                       (std::dynamic_pointer_cast<SQLIdentifierExpr>(column))->nameHashCode64() == columnNameHashCode64)
    {
      return BOOL::TRUE;
    }
  }
  return BOOL::FALSE;
}
